package com.web.Controller;

import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.apache.shiro.crypto.hash.Md5Hash;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.web.config.RegVal;

public class UsermgController extends Controller{
	private static Logger logger = Logger.getLogger(UsermgController.class);
	public void index() {
		setAttr("errstr", "请认真填写");
		render("/usermg/index.html");
	}
	public void dosave() {
		String errstr = "";
		String username = getPara("username" , "").trim();
		Long i =Db.queryLong("select count(*) from users where  username=?",username);
		String card = getPara("card" , "0370").trim().toUpperCase();
		 if(!Pattern.matches("^[0-9]{0,10}$",getPara("push" , "") )){
			 errstr ="推荐人ID需为整数";
		 }
		 String password=getPara("password","123456").trim();
		if(!RegVal.isPassword(password)){
			errstr ="密码需为6--16位";
		}
		if(getPara("real_name","").isEmpty()){
			errstr ="姓名不能为空";
		}
		if(!RegVal.isIDCard(card)){
			errstr ="身份证输入有误";
		}
		if(i >0){
			errstr ="该手机号已被注册";
		}
		if(!RegVal.isMobile(username)){
			errstr ="手机号输入有误";
			logger.info(username);
		}
		PropKit.use("jfinal.properties");
		String salt=PropKit.get("salt");
		password= new Md5Hash(password, salt).toString();
		Record users =new Record();
		users.set("username", getPara("username"))
				.set("password", password).set("real_name", getPara("real_name"))
				.set("card", card).set("short_name", getPara("short_name"))
				.set("sex", getPara("sex")).set("birthday", getPara("birthday"))
				.set("high", getParaToInt("high",1)).set("weight",getParaToInt("weight",1) )
				.set("education", getPara("education")).set("hometown",getPara("hometown") )
				.set("work", getPara("work")).set("workaddr", getPara("workaddr"))
				.set("myself", getPara("myself")).set("others", getPara("others"))
				.set("push", getParaToInt("push",0)).set("fang", getPara("fang"))
				.set("che", getPara("che")).set("top",getPara("top") );
		boolean	f=Db.save("users", users);
		renderJson("{state:\"success\",msg:\"e司法所e！\"}");
		return;
//		if(f){
//			setAttr("msgid", Db.queryInt("select id from users order by id desc limit 1"));
////			render("/usermg/suc.html");
//			renderJson("{state:\"success\",msg:\"评论成功！\"}");
//			return;
//		}else{
//			renderJson("{state:\"success\",msg:\"评dsa论成功！\"}");
//			return;
//		}
//		if(errstr.isEmpty()){
//			
//		}else{
//			setAttr("errstr",errstr);
//			
////			render("/usermg/index.html");
//		}
	}
}
